<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!--{include file='comm/header.tpl'}-->
    <title>调度星盘</title>
    <!--{include file='comm/comm_cal.tpl'}-->
    <!--{include file='comm/comm_region.tpl'}-->
    <!--{include file="comm/icheck.tpl"}-->
    <link rel="stylesheet" href="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.css" />

    <style>
        .BMap_contextMenu{
            width: 200px!important;
        }
        .BMap_contextMenu div{
            width: 196px!important;
        }
    </style>

</head>
<body>

<div class="container">
    
        <div class="comm_content">

            <div class="search-box">
                <div style="position:relative">
                    <form class="form-inline" id="search_form" onsubmit="return false;">
                        <table>
                            <tbody>
                            <tr>
                                <td>
                                    <span>配送日期：<input type="text" readonly class="form-control input-sm" style="width:110px" value="<!--{$currentDate|default:''}-->" name="send_date" id="send_date"></span>
                                    <!--<button type="button" class="btn btn-default btn-sm" onclick="schedulingChart.timeClear();">清 空</button>-->
                                    <span>
                                        配送开始时间：
                                        <select name="send_start_time" id="send_start_time" class="form-control input-sm" style="width:100px;">
                                            <option value="">请选择</option>
                                            <!--{foreach from = $sendStartTime_array key=myId item = val}-->
                                            <!--{if $myId == 0}-->
                                            <option value="<!--{$val.itemName}-->" selected><!--{$val.itemName}--></option>
                                            <!--{/if}-->
                                            <option value="<!--{$val.itemName}-->"><!--{$val.itemName}--></option>
                                            <!--{/foreach}-->

                                        </select>
                                    </span>

                                    <span>
                                        配送结束时间：
                                        <select name="send_start_time_end" id="send_start_time_end" class="form-control input-sm" style="width:100px;">
                                            <option value="">请选择</option>
                                            <!--{foreach from = $sendEndTime_array item = val}-->
                                            <!--{if $val.itemName == $end_time}-->
                                            <option value="<!--{$val.itemName}-->" selected><!--{$val.itemName}--></option>
                                            <!--{/if}-->
                                            <option value="<!--{$val.itemName}-->"><!--{$val.itemName}--></option>
                                            <!--{/foreach}-->
                                        </select>
                                    </span>

                                    <span>
                                       配送城市：<select id="deliver_cityid" name="deliver_cityid" class="form-control input-sm" style="width:100px;">
                                            <option value="">请选择</option>
                                        　　

                                        </select>
                                    </span>

                                    <span>
                                        配送站：
                                        <select name="distribution_id" id="distribution_id" class="form-control input-sm" style="width:140px;">
                                            <option value="">请选择</option>
                                        </select>
                                    </span>

                                    <span>
                                        配送员：
                                        <select name="distribution_technician_id" id="distribution_technician_id" class="form-control input-sm" style="width:140px;">
                                            <option value="">请选择</option>
                                        </select>
                                    </span>

                                    <span>订单编号：<input type="text" class="form-control input-sm" style="width:150px" value="" name="bill_code" id="bill_code"></span>

                                    <span><input type="button" onclick="schedulingChart.search();" class="btn btn-primary" value="查询"></span>
                                    <span><input type="button" onclick="schedulingChart.reSet();" class="btn btn-warning" value="重置"></span>

                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </form>
                </div>
            </div>

            <div id="show_map">

                <div id="allmap" class="bdmap" style="height:500px; margin: 0px auto;"></div>

            </div>

        </div>


</div>


<script type="text/javascript" src="/inc/config/default/region.min.js"></script>
<script type="text/javascript" src="/framework/ebsig/js/region.change.min.js"></script>
<!--baidu map-->
<!--{include file="comm/baidu_map.tpl"}-->

<script type="text/javascript">
ZoomNum = -1; //地图缩放级别
centerP = -1; //地图当前中心点
    var schedulingChart = {

        marker:[],  //标注数组

        //查询
        search:function(){
            var formValues = E.getFormValues('search_form');
            var send_date = $('#send_date').val();//配送日期
            var deliver_cityid = $('#deliver_cityid').val();//城市ID

            var distribution_id = $('#distribution_id').val();//配送站id

            var distribution_id_html = $('#distribution_id').html();//配送站的内容
            var error_msg = ''; //错误信息
            if(send_date == ''){
                error_msg += '配送日期不能为空<br>';
            }else if( !E.isDate( send_date ) ){
                error_msg += '配送日期必须为日期格式<br>';
            }

            if(deliver_cityid == ''){
                error_msg += '配送城市必须选择<br>';
            }

            if(distribution_id =='' || distribution_id_html ==''){
                error_msg += '配送站必须选择<br>';
            }

            if( error_msg ){
                E.alert( error_msg );
            }else{
                var formValues = E.getFormValues('search_form');
                E.ajax_post({
                    url: '/invokingAjax-backend/send/schedulingChart.ajax?operFlg=1',
                    data: {
                        formValues: formValues
                    },
                    call: function( o ) {

                        if (o.code == 200) {
                            $('#show_map').show();
                            showMap(o.data);
                        }else{
                            $('#show_map').hide();
                            E.alert(o.message);

                        }

                    }
                });
            }
        },


        //弹出层中选择配送员联动获得配送员手机号
        showMobile:function (){
            var distribution_technician_id = $('#distribution_technician_id_up').val();//配送员id
            E.ajax_post({
                url: '/invokingAjax-backend/send/schedulingChart.ajax?operFlg=2',
                data: {
                    distribution_technician_id: distribution_technician_id
                },
                call: function( o ) {

                    if (o.code == 200) {
                        var arrLen = o.data.length;
                        if( arrLen > 1  ){
                            $('#mobile_up').val('');
                        }else{
                            $('#mobile_up').val(o.data[0].mobile);
                        }

                    }

                }
            });

        },


        //修改配送员
        editDistribution:function(){
            var distribution_technician_id = $('#distribution_technician_id_up').val();//配送员id
            var distribution_technicia = $('#distribution_technician_id_up').find('option:selected').text();//配送员名称
            var bill_no = $('#bill_no_up').val();
            var mobile = $('#mobile_up').val();
            if (distribution_technician_id == '') {
                E.alert('暂无配送员,无法修改');
            }else{
                E.ajax_post({
                    url: '/invokingAjax-backend/send/schedulingChart.ajax?operFlg=3',
                    data: {
                        distribution_technician_id: distribution_technician_id,
                        distribution_technicia:distribution_technicia,
                        bill_no: bill_no,
                        mobile: mobile
                    },
                    call: function( o ) {
                        E.popup.close();
                        if (o.code == 200) {
                            var formValues = E.getFormValues('search_form');
                            E.ajax_post({
                                url: '/invokingAjax-backend/send/schedulingChart.ajax?operFlg=1',
                                data: {
                                    formValues: formValues
                                },
                                call: function( o ) {

                                    if (o.code == 200) {
                                        $('#show_map').show();
                                        showMap(o.data);
                                    }else{
                                        $('#show_map').hide();
                                        E.alert(o.message);

                                    }

                                }
                            });
                            E.alert("配送信息修改成功",2);

                        }else{
                            $('#show_map').hide();
                            E.alert(o.message);
                        }

                    }
                });
            }
        },

        //重置
        reSet:function(){
            document.getElementById('search_form').reset();
            $('#distribution_id').val('');
            $('#distribution_technician_id').val('');
            $('#show_map').hide();
        },

        timeClear: function() {
            $('#send_date').val('');
        }

    };


    //日期控件JS
    $('#send_date').datepicker();



    // 百度地图API功能
    function showMap( msg,zoomNum,centerPoint){
        if( zoomNum == undefined ){
            if ( ZoomNum == -1 ) {
                zoomNum = 11;
            } else {
                zoomNum = ZoomNum;
            }
        }

        if( centerPoint == undefined ){
            if ( centerP == -1 ) {
                centerPoint = $('#deliver_cityid').find('option:selected').text();
            }else{
                centerPoint = centerP;
            }
        }

        var map = new BMap.Map('allmap', {enableMapClick:false});
        var myGeo = new BMap.Geocoder('allmap');
        // 将地址解析结果显示在地图上，并调整地图视野

        map.enableScrollWheelZoom();

        //var centerPoint = $('#deliver_cityid').find('option:selected').text();

        map.centerAndZoom(centerPoint, zoomNum);
        ZoomNum =zoomNum;
        centerP = centerPoint;
        $.each( msg['billMaster'], function( k, v) {
            var address_str = v.deliver_province + v.deliver_city + v.deliver_county + v.deliver_address;
            myGeo.getPoint( address_str, function(point){
                if (point) {

                    //=========添加图形标注start====================
                    if( v.send_start_time <= "12:00:00" && v.send_start_time != '00:00:00'){
                        var myIcon = new BMap.Icon("/quanliadmin/docroot/images/system/A.png", new BMap.Size(32, 25), {
                            anchor: new BMap.Size(10, 25),
                            infoWindowAnchor: new BMap.Size(10, 0)
                        });
                    }else{
                        var myIcon = new BMap.Icon("/quanliadmin/docroot/images/system/P.png", new BMap.Size(32, 25), {
                            anchor: new BMap.Size(10, 25),
                            infoWindowAnchor: new BMap.Size(10, 0)
                        });
                    }
                    //=========添加图形标注end====================

                    //=========添加文本标注start====================



                    var opts = {
                        position : point,    // 指定文本标注所在的地理位置
                        //mod by allen.qiang 20141014 允许拖拽坐标，达到显示所有订单 --start
                        //anchor : new BMap.Size(25, -30)    //设置文本偏移量
                        offset : new BMap.Size(10,25)    //设置文本偏移量
                        //mod by allen.qiang 20141014 允许拖拽坐标，达到显示所有订单 --end
                    }

                    if( !v.distribution_technician_id ){
                        var label = new BMap.Label(v.bill_code_sub + "&nbsp;&nbsp;" + v.send_start_time + "&nbsp;&nbsp; 配送员未分配", opts);  // 创建文本标注对象
                        label.setStyle({
                            color : "black",
                            fontSize : "12px",
                            height : "20px",
                            background :'#FE3B3B',
                            lineHeight : "20px",
                            fontFamily:"微软雅黑",
                            border : '1px solid red',
                            margin_top: '-200px'
                        });
                    }else{
                        var label = new BMap.Label(v.bill_code_sub + "&nbsp;&nbsp;" + v.send_start_time + "&nbsp;&nbsp;"+v.distribution_technicia, opts);  // 创建文本标注对象
                        label.setStyle({
                            color : "black",
                            fontSize : "12px",
                            height : "20px",
                            background :'#16F910',
                            lineHeight : "20px",
                            fontFamily:"微软雅黑",
                            border : '1px solid green',
                            margin_top: '-200px'
                        });
                    }
                    //del by allen.qiang 20141014 允许拖拽坐标，达到显示所有订单 --start
                    //map.addOverlay(label);
                    //del by allen.qiang 20141014 允许拖拽坐标，达到显示所有订单 --end
                    //=========添加文本标注end====================

                    schedulingChart.marker[v.bill_no] = new BMap.Marker(point, {icon: myIcon});
                    //add by allen.qiang 20141014 允许拖拽坐标，达到显示所有订单 --start
                    schedulingChart.marker[v.bill_no].enableDragging();//可拖拽
                    schedulingChart.marker[v.bill_no].setLabel(label); //标注上建LABEL
                    //add by allen.qiang 20141014 允许拖拽坐标，达到显示所有订单 --end
                    map.addOverlay(schedulingChart.marker[v.bill_no]);

                    var html = '<div>';
                    html += '<div class="red" style="font-size: 14px;">配送信息</div>';
                    html += '<input type="hidden" id="bill_no_up" value="'+ v.bill_no +'">';
                    html += '<div>订单编号：'+ v.bill_code+'</div>';
                    html += '<div>收货地址：'+ address_str+'</div>';
                    if (v.send_end_time) {
                        html += '<div>配送时间：'+ v.send_start_time + ' - ' + v.send_end_time +'</div>';
                    } else {
                        html += '<div>配送时间：'+ v.send_start_time +'</div>';
                    }
                    html += '<div>';
                    html += '配送员：';

                    if(v.bill_status == 7){
                        html += '<select style="width: 70px;" disabled  onchange="schedulingChart.showMobile()" id="distribution_technician_id_up">';
                    }else{
                        html += '<select style="width: 70px;"  onchange="schedulingChart.showMobile()" id="distribution_technician_id_up">';
                    }

                    html += '</select>';
                    html += '</div>';
                    html += '<div>';
                    html += '手机号：';
                    html += '<input type="text"  readonly id="mobile_up" value="" style="width:90px;margin-top: 4px;margin-right: 3px;line-height: 20px;" >';
                    if(v.bill_status == 7){
                        html += '<input type="text" readonly class="zoom_value" name="zoom_value" value="" style="display: none">'
                    }else{
                        html += '<input type="text" class="zoom_value" name="zoom_value" value="" style="display: none">'
                    }
                    if(v.bill_status == 7){
                        html += '<span><input style="display: none;" type="button" id="sure_btn" value="确定" onclick="schedulingChart.editDistribution()" class="btn btn-xs btn-primary"></span>';
                    }else{
                        html += '<span><input type="button" id="sure_btn" value="确定" onclick="schedulingChart.editDistribution()" class="btn btn-xs btn-primary"></span>';
                    }
                    html += '</div>';
                    html += '</div>';
                    var infoWindow = new BMap.InfoWindow(html, {enableMessage:false });

                    schedulingChart.marker[v.bill_no].addEventListener("mouseover", function(){
                        ZoomNum  = map.getZoom();//地图缩放级别
                        centerP = map.getCenter();//地图中心点
                        this.openInfoWindow(infoWindow);
                        //获得当前配送站下所有配送员
                        var technicianHtml ='';
                        var distribution_id = v.distribution_id;
                        E.ajax_post({
                            url: "/invokingAjax-comm/bill/distribution.ajax?operFlg=3",
                            data: {distribution_id: distribution_id},
                            call: function( o ) {

                                if(o.code ==200 && o.data != null){
                                    technicianHtml +='<option value="0">请选择</option>';
                                    $.each(o.data, function(i,val){
                                        if(val.distribution_technician_id ==  v.distribution_technician_id){
                                            technicianHtml +='<option  selected value="'+val.distribution_technician_id+'">'+val.distribution_technician_name+'</option>';
                                            $('#mobile_up').val(val.mobile);
                                        }else{
                                            technicianHtml +='<option value="'+val.distribution_technician_id+'">'+val.distribution_technician_name+'</option>';
                                        }

                                    });
                                    $('#distribution_technician_id_up').html( technicianHtml );


                                }else{
                                    technicianHtml += '';
                                    $('#distribution_technician_id_up').html(technicianHtml);
                                }
                            }
                        });

                    });

                }
            }, v.deliver_city);


        });

        //添加默认缩放平移控件
        map.addControl(new BMap.NavigationControl());

        //===================右键菜单显示配送站数据统计start===================
        var menu = new BMap.ContextMenu();
        var v = msg['bill_goods_count'];
        var txtMenuItem = [
            {
                text:'<h2><p style="color: red;">订单汇总</p></h2>',
                call:function(){}
            },
            {
                text:'配送站：'+ v.distribution_name+'',
                call:function(){}
            },
            {
                text:'订单总额：'+ v.count_bill_money+'',
                call:function(){}
            },
            {
                text:'应收金额：'+ v.count_bill_receivable_money+'',
                call:function(){}
            },{
                text:'订单总数：'+ v.count_bill_totalAmount+'',
                call:function(){}
            },
            {
                text:'已分配总数：'+ v.already_count_bill_totalAmount+'',
                call:function(){}
            },
            {
                text:'未分配总数：'+ v.not_count_bill_totalAmount+'',
                call:function(){}
            },{
                text:'已分配磅数：'+ v.already_count_goods_spec +'',
                call:function(){}
            },
            {
                text:'未分配磅数：'+ v.not_count_goods_spec+'',
                call:function(){}
            }

        ];
        for(var i=0; i < txtMenuItem.length; i++){
            menu.addItem(new BMap.MenuItem(txtMenuItem[i].text,txtMenuItem[i].call,100));
        }
        map.addContextMenu(menu);
        //===================右键菜单显示配送站数据统计end===================
    }


    $(window).ready(function() {

        $("#deliver_cityid").html("");

        var cityHtml = '<option value="">请选择</option>';

        $.ajax({
            type: "post",
            url: "/invokingAjax-comm/bill/distribution.ajax?operFlg=1",
            dataType: "json",
            data: "",
            success: function( result_obj ) {
                if(result_obj.code == 200){
                    var selected = "";
                    if(result_obj.data.length == 1){
                        selected = "selected";
                    }
                    $.each(result_obj.data, function(i,val){
                        cityHtml +='<option value="'+val.id+'" '+selected+'>'+val.name+'</option>';
                    });
                    $('#deliver_cityid').html(cityHtml);
                    $("#deliver_cityid").trigger("change");
                }else{
                    E.alert(result_obj.message);
                }
            }
        });


        //点击城市联动获得配送站
        $(document).on('change', '#deliver_cityid', function() {

            $('#distribution_id').html("");
            $('#distribution_technician_id').html('');

            var deliver_cityid = $('#deliver_cityid').children('option:selected').val();
            var distributionHtml = '<option value="">请选择</option>';
            var technicianHtml = '<option value="">请选择</option>';
            $.ajax({
                type: "post",
                url: "/invokingAjax-comm/bill/distribution.ajax?operFlg=2",
                dataType: "json",
                data: {deliver_cityid: deliver_cityid},
                success: function( obj ) {
                    if(obj.code ==200 && obj.data != null){
                        var selected = "";
                        if(obj.data.length == 1){
                            selected = "selected";
                        }
                        $.each(obj.data, function(i,val){
                            distributionHtml +='<option value="'+val.distribution_id+'" '+selected+'>'+val.distribution_name+'</option>';
                        });
                        $('#distribution_id').html(distributionHtml);
                        $('#distribution_technician_id').html(technicianHtml);
                    }else{
                        distributionHtml += '';
                        $('#distribution_id').html(distributionHtml);
                        $('#distribution_technician_id').html(technicianHtml);
                    }
                    $("#distribution_id").trigger("change");
                }
            });
        });

        //点击配送站联动获得配送员
        $(document).on('change', '#distribution_id', function() {

            $('#distribution_technician_id').html('');
            var distribution_id = $('#distribution_id').children('option:selected').val();
            var technicianHtml = '<option value="">请选择</option>';
            $.ajax({
                type: "post",
                url: "/invokingAjax-comm/bill/distribution.ajax?operFlg=3",
                dataType: "json",
                data: {distribution_id: distribution_id},
                success: function( o ) {
                    if(o.code ==200 && o.data != null){
                        $.each(o.data, function(i,val){
                            technicianHtml +='<option value="'+val.distribution_technician_id+'">'+val.distribution_technician_name+'</option>';
                        });
                        $('#distribution_technician_id').html(technicianHtml);
                    }else{
                        technicianHtml += '';
                        $('#distribution_technician_id').html(technicianHtml);
                    }
                }
            });
        });

    });
</script>

</body>
</html>